package com.group.model;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.group.util.NoteException;
import com.group.util.WeiXinUtils;

/**
 * 根据token获取用户信息
 * 
 * @author zhaolei
 * 
 */
public class WeiXinUser {
	private static final Logger logger = LoggerFactory.getLogger(WeiXinUser.class);

	private Long id;

	private String openid;

	private String unionid;

	private String nickname;
	
	private Long commentId;

	/**
	 * 1 男,2 女 , 0 人妖
	 */
	private String sex;

	private String province;

	private String city;

	private String country;
	/**
	 * 用户头像,无头像为空
	 */
	private String headimgurl;

	/**
	 * 用户特权信息，json 数组，如微信沃卡用户为（chinaunicom）
	 */
	private String privilege;

	private Date createTime;

	/**
	 * 是否关注,1关注,0未关注
	 */
	private Boolean subscribe;

	public WeiXinUser() {

	}

	/**
	 * 刷新用户信息
	 * 
	 * @param wu
	 */
	public void refesh(WeiXinUser wu) {
		this.city = wu.getCity();
		this.country = wu.getCountry();
		this.headimgurl = wu.getHeadimgurl();
		this.nickname = wu.getNickname();
		this.privilege = wu.getPrivilege();
		this.province = wu.getProvince();
		this.sex = wu.getSex();
		this.unionid = wu.getUnionid();
		this.openid = wu.openid;
	}

	/**
	 * 从微信端生成user
	 * 
	 * @param access_token
	 * @param openid
	 */
	public WeiXinUser(String access_token, String openid) {
		String response = WeiXinUtils.get("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN");

		JSONObject wxUser = JSONObject.parseObject(response);
		logger.info("weixinuser response xml= " + response);

		this.setOpenid(wxUser.getString("openid"));
		this.setCity(wxUser.getString("city"));
		this.setCountry(wxUser.getString("country"));
		this.setHeadimgurl(wxUser.getString("headimgurl"));
		this.setNickname(wxUser.getString("nickname"));
		this.setPrivilege(wxUser.getString("privilege"));
		this.setProvince(wxUser.getString("province"));
		this.setSex(wxUser.getString("sex"));
		this.setUnionid(wxUser.getString("unionid"));

		// 验证是否正确
		if (StringUtils.isEmpty(this.getOpenid()))
			throw new NoteException("获取user信息失败");
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getHeadimgurl() {
		return headimgurl;
	}

	public void setHeadimgurl(String headimgurl) {
		this.headimgurl = headimgurl;
	}

	public String getPrivilege() {
		return privilege;
	}

	public void setPrivilege(String privilege) {
		this.privilege = privilege;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Boolean getSubscribe() {
		return subscribe;
	}

	public void setSubscribe(Boolean subscribe) {
		this.subscribe = subscribe;
	}

	public String getUnionid() {
		return unionid;
	}

	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}

	@Override
	public String toString() {
		return "WeiXinUser [id=" + id + ", openid=" + openid + ", unionid=" + unionid + ", nickname=" + nickname + ", sex=" + sex + ", province=" + province + ", city=" + city + ", country=" + country
				+ ", headimgurl=" + headimgurl + ", privilege=" + privilege + ", createTime=" + createTime + ", subscribe=" + subscribe + "]";
	}

	public Long getCommentId() {
		return commentId;
	}

	public void setCommentId(Long commentId) {
		this.commentId = commentId;
	}

}
